<?php
class Logic_Bug extends CMF_Krypton_Core_Logic
{
	public static function addBug( $data, $prefix = NULL )
	{
		$data	= self::removePrefixFromFields( $data, $prefix );
		self::validateForm( 'bug', 'addBug', $data, $prefix );
		$parts		= explode( ":", $data['project'] );
		$data['serverId']	= $parts[0];
		$data['projectId']	= $parts[1];
		$data['created']	= time();
		$model	= new Model_Bug();
		return $model->insert( $data );
	}

	/**
	 *	Returns total amount of Bug Reports.
	 *	@access		public
	 *	@return		int
	 */
	public static function getBugCount()
	{
		$dbc	= $this->registry->get( 'dbc' );
		$sb		= new StatementBuilder( $this->getPrefix() );
		$sb->addTable( "bugs" );
		$sb->addKey( "bug_id" );
		$sql	= $sb->buildCountStatement();
		$res	= $dbc->execute( $sql );
		$data	= $res->fetchArray();
		return $data['rowcount'];
	}

	/**
	 *	Returns the Bug ID of next Bug.
	 *	@access		public
	 *	@param		int		$bug_id			Current Bug ID
	 *	@return		int
	 */
	public static function getNextBug( $bug_id )
	{
		$dbc	= $this->registry->get( 'dbc' );
		$sb		= new Database_StatementBuilder( $this->getPrefix() );
		$bs		= new Collection_Bug( $sb );
		$bs->nextBug( $bug_id );
		$sb->addTable( "bugs" );
		$sb->addKey( "bug_id" );
		$sql	= $sb->buildSelectStatement();
		$res	= $dbc->execute( $sql );
		$data	= $res->fetchArray();
		if( count( $data ) )
			return $data['bug_id'];
		return FALSE;
	}

	/**
	 *	Returns the Bug ID of previous Bug.
	 *	@access		public
	 *	@param		int		$bug_id			Current Bug ID
	 *	@return		int
	 */
	public static function getPreviousBug( $bug_id )
	{
		$dbc	= $this->registry->get( 'dbc' );
		$sb		= new Database_StatementBuilder( $this->getPrefix() );
		$bs		= new Collection_Bug( $sb );
		$bs->previousBug( $bug_id );
		$sb->addTable( "bugs" );
		$sb->addKey( "bug_id" );
		$sql	= $sb->buildSelectStatement();
		$res	= $dbc->execute( $sql );
		$data	= $res->fetchArray();
		if( count( $data ) )
			return $data['bug_id'];
		return FALSE;
	}

	/**
	 *	Indicates whether an User is Owner of current Bug.
	 *	@access		public
	 *	@param		int		$user_id		User ID
	 *	@return		bool
	 */
	public static function isOwner( $bugId, $userId )
	{
	}

	/**
	 *	Indicates whether an User is Owner of a Bug.
	 *	@access		public
	 *	@param		int		$user_id		User ID
	 *	@param		int		$bug_id			Bug ID
	 *	@return		bool
	 */
	public static function isOwnerOfBug( $user_id, $bug_id )
	{
		$model	= new Model_Bug( $bugId );
		$model->focusIndex( 'user_id', $userId );
		return (bool) $model->getData( TRUE );
	}
}
?>